package com.sdhs.archivesmanage.service.impl;

import java.util.List;

import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.common.utils.DateUtils;
import com.sdhs.projectschedule.domain.GcProgressConstructionLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sdhs.archivesmanage.mapper.GcFileManagementMapper;
import com.sdhs.archivesmanage.domain.GcFileManagement;
import com.sdhs.archivesmanage.service.IGcFileManagementService;

/**
 * 档案管理Service业务层处理
 *
 * @author andong
 * @date 2025-04-10
 */
@Service
public class GcFileManagementServiceImpl implements IGcFileManagementService
{
    @Autowired
    private GcFileManagementMapper gcFileManagementMapper;

    @Autowired
    private IBusinessFileService businessFileService;
    /**
     * 查询档案管理
     *
     * @param id 档案管理主键
     * @return 档案管理
     */
    @Override
    public GcFileManagement selectGcFileManagementById(Long id)
    {
        GcFileManagement gcFileManagement = gcFileManagementMapper.selectGcFileManagementById(id);
        List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GC_FILE_MANAGEMENT.getCode(), String.valueOf(id));
        gcFileManagement.setFileList(businessFiles);
        return gcFileManagement;
    }

    /**
     * 查询档案管理列表
     *
     * @param gcFileManagement 档案管理
     * @return 档案管理
     */
    @Override
    public List<GcFileManagement> selectGcFileManagementList(GcFileManagement gcFileManagement)
    {
        return gcFileManagementMapper.selectGcFileManagementList(gcFileManagement);
    }

    /**
     * 新增档案管理
     *
     * @param gcFileManagement 档案管理
     * @return 结果
     */
    @Override
    public int insertGcFileManagement(GcFileManagement gcFileManagement)
    {
		gcFileManagement.setCreater(SecurityUtils.getUsername());
        gcFileManagement.setCreateTime(DateUtils.getNowDate());
        gcFileManagement.setSubmitTime(DateUtils.getNowDate());
        int i = gcFileManagementMapper.insertGcFileManagement(gcFileManagement);
        handler(gcFileManagement.getId(),gcFileManagement);
        return i;
    }

    /**
     * 修改档案管理
     *
     * @param gcFileManagement 档案管理
     * @return 结果
     */
    @Override
    public int updateGcFileManagement(GcFileManagement gcFileManagement)
    {
		gcFileManagement.setUpdater(SecurityUtils.getUsername());
        gcFileManagement.setUpdateTime(DateUtils.getNowDate());
        gcFileManagement.setSubmitTime(DateUtils.getNowDate());
        int i = gcFileManagementMapper.updateGcFileManagement(gcFileManagement);
        handler(gcFileManagement.getId(),gcFileManagement);
        return i;
    }

    /**
     * 批量删除档案管理
     *
     * @param ids 需要删除的档案管理主键
     * @return 结果
     */
    @Override
    public int deleteGcFileManagementByIds(Long[] ids)
    {
        return gcFileManagementMapper.deleteGcFileManagementByIds(ids);
    }

    /**
     * 删除档案管理信息
     *
     * @param id 档案管理主键
     * @return 结果
     */
    @Override
    public int deleteGcFileManagementById(Long id)
    {
        return gcFileManagementMapper.deleteGcFileManagementById(id);
    }

    /**
     * 保存联合数据
     * 保存合同附件
     */
    public void handler(Long id,GcFileManagement gcFileManagement) {
        // 保存合同附件
        businessFileService.saveFileBatch(BusinessTypeEnum.GC_FILE_MANAGEMENT.getCode(), String.valueOf(id), gcFileManagement.getFileList());
    }
}
